﻿using Module = MicroCloud.Authorization.Entities.Module;

namespace MicroCloud.Authorization.Database
{
    #region "模块信息数据实体映射配置"
    /// <summary>
    /// 模块信息数据实体映射配置
    /// </summary>
    public class ModuleConfiguration : EntityTypeConfigurationBase<Module, long>
    {
        #region "方法"
        #region "重写以实现实体类型各个属性的数据库配置"
        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<Module> builder)
        {
            builder.HasIndex(m => new { m.ParentId, m.Name }).SetIndex("Name", true).HasFilter(null);
            builder.HasIndex(m => new { m.ParentId, m.Code }).SetIndex("Code", true).HasFilter(null);
            switch (DbContextOptions?.DatabaseType)
            {
                case DatabaseType.SqlServer:
                case DatabaseType.PostgreSql:
                case DatabaseType.Oracle:
                    builder.HasIndex(m => m.FullName).SetIndex("FullName", true);
                    builder.HasIndex(m => m.FullCode).SetIndex("FullCode", true);
                    builder.HasIndex(m => m.TreePathString).SetIndex("TreePathString", true).HasFilter(null);
                    break;
            }

            builder.HasOne(m => m.Parent).WithMany(m => m.Children).HasForeignKey(m => m.ParentId).SetForeignKey("ParentId", false);

            ConfigurationAppend(builder);
        }
        #endregion

        #endregion

    }
    #endregion

}
